package asf.forms
{
	import asf.core.Application;
	
	import br.com.stimuli.loading.BulkLoader;
	
	import flash.utils.*;

	dynamic public class Forms extends Proxy
	{
		private var raw:XML;
		private var app:Application;
		private var items:Array;
		private var dict:Object;
		private var dependencies:BulkLoader;
		
		public function Forms( p_raw:XML, p_app:Application, p_dependencies:BulkLoader )
		{
			super( );
			
			raw = p_raw;
			app = p_app;
			dependencies = p_dependencies;
			
			parse( );
		}
		
		private function parse( ):void
		{
			var formItemXML:XML;
			var item:Form;
			
			items = new Array( );
			dict = new Object( );
			
			for each( formItemXML in raw.form )
			{
				item = new Form( formItemXML, app, dependencies.getXML( formItemXML.@id || formItemXML.id ) );
				
				items.push( item );
				dict[ item.id ] = item;
			}
		}
		
		public function get forms( ):Array
		{
			return [ ].concat( items );
		}
		
		override flash_proxy function getProperty( name:* ):*
		{
			return getForm( name );
		}
		
		public function getForm( formID:String ):Form
		{
			return dict[ formID ];
		}
		
		public function dispose( ):void
		{
			var item:Form;
			
			for each( item in items )
			{
				item.dispose( );
			}
			
			dependencies = null;
			items = null;
			dict = null;
		}
	}
}